/**
 * Created by chengxiaolong on 2016/5/27.
 */
package com.invexer.baosteel.cashflow.mvc.mediator {
import com.invexer.baosteel.app.model.BaosteelConfigModelBase;
import com.invexer.baosteel.cashflow.mvc.constant.CashflowCommandConst;
import com.invexer.baosteel.cashflow.mvc.constant.CashflowNotificationConst;
import com.invexer.baosteel.cashflow.mvc.model.CashflowConfigModel;
import com.invexer.baosteel.cashflow.mvc.model.CashflowModel;
import com.invexer.baosteel.cashflow.mvc.view.CashflowView;
import com.invexer.common.puremvc.patterns.mediator.InvexerSimpleMediatorBase;

import flash.events.Event;
import flash.utils.clearInterval;
import flash.utils.setInterval;

import org.puremvc.as3.multicore.interfaces.INotification;

public class CashflowMdt extends InvexerSimpleMediatorBase{
    public static const NAME:String	= "CashflowMdt";


    private var _cashflowModel:CashflowModel;

    private var _scheduleJob:uint;

    public function CashflowMdt(viewComponent:Object ){
        super( NAME, viewComponent );
    }

    override public function onRegister( ):void{
        super.onRegister();

        initModels();
        addUIEventListener();
        initScheduleJob();
    }

    override public function onRemove():void {
        super.onRemove();
        removeUIEventListener();
        finalizeModels();
        finalizeScheduleJob();
    }

    override public function listNotificationInterests():Array{
        return [
            CashflowNotificationConst.CashFlowUpdateData,
        ];
    }

    override public function handleNotification(note:INotification):void{
        switch ( note.getName() ) {
            case CashflowNotificationConst.CashFlowUpdateData:
                view.total = _cashflowModel.total;
                view.icbc = _cashflowModel.icbc;
                view.ccb = _cashflowModel.ccb;
                break;
        }
    }

    private function get view():CashflowView{
        return getViewComponent() as CashflowView;
    }


    private function handleEvent( event:Event ):void{
        switch ( event.type ){

        }
    }

    private function addUIEventListener():void{
    }

    private function removeUIEventListener():void{
    }

    private function initModels():void{
        _cashflowModel = facade.retrieveProxy(CashflowModel.NAME) as CashflowModel;
    }

    private function finalizeModels():void{
        _cashflowModel = null;
    }

    private function initScheduleJob():void{
        queryData();

        var configModel:BaosteelConfigModelBase = facade.retrieveProxy(CashflowConfigModel.NAME) as BaosteelConfigModelBase;

        _scheduleJob = setInterval(function () {
            queryData();
        }, configModel.refreshInternal);
    }

    private function finalizeScheduleJob():void{
        clearInterval(_scheduleJob);
    }

    private function queryData():void{
        sendNotification(CashflowCommandConst.CashFlowGetDataCMD);
    }


}
}
